package com.cuotigou.service.impl;

import com.cuotigou.mapper.SLabelMapper;
import com.cuotigou.mapper.SSubjectMapper;
import com.cuotigou.mapper.SUserMapper;
import com.cuotigou.pojo.SLabel;
import com.cuotigou.pojo.SSubject;
import com.cuotigou.pojo.SUser;
import com.cuotigou.service.LabelService;
import com.cuotigou.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * Created by Administrator on 2018/11/11.
 */
@Service
@Transactional
public class LabelServiceImpl implements LabelService {

    private static final Logger log = LoggerFactory.getLogger(LabelServiceImpl.class);

    @Autowired
    private SLabelMapper sLabelMapper;
    @Autowired
    private SSubjectMapper sSubjectMapper;
    @Autowired
    private SUserMapper sUserMapper;

    @Override
    public ObjectResult wrongTopicLabel(XMLBean xmlBean) {
        String userId =CommonUtils.decToken(xmlBean.getToken());

        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));
        Map<String,Object>  map = new HashMap<>();
        map.put("userId",userId);
        map.put("type","(1,2,3,4,5,6)");
        map.put("gradeId",sUser.getGradeId());

        List<SLabel> labels = sLabelMapper.selectWrongTopicLabeLByMap(map);

        List<SLabel> list1 = new ArrayList<>();
        List<SLabel> list2 = new ArrayList<>();
        List<SLabel> list3 = new ArrayList<>();
        List<SLabel> list4 = new ArrayList<>();
        List<SLabel> list5 = new ArrayList<>();
        List<SLabel> list6 = new ArrayList<>();

        for (SLabel label:labels){
            switch (label.getType()){
                case 1:
                    List<SSubject> list = sSubjectMapper.selectSubjectListByUserId(userId);
                    //只返回学生年级包含的科目
                    for (SSubject sSubject:list){
                        if (label.getTitle().equals(sSubject.getTitle())) list1.add(label);
                    }
                    break;
                case 2:
                    list2.add(label);break;
                case 3:
                    list3.add(label);break;
                case 4:
                    list4.add(label);break;
                case 5:
                    list5.add(label);break;
                case 6:
                    list6.add(label);break;
                default:break;
            }
        }

        //返回添加标签
        SLabel label1 = new SLabel();
        label1.setId(0l);
        label1.setTitle("添加");
        label1.setUserId(0);
        list3.add(label1);
        list4.add(label1);
        list5.add(label1);

        map.clear();
        map.put("list1",list1);//选着科目
        map.put("list2",list2);//错题类型
        map.put("list3",list3);//错题原因
        map.put("list4",list4);//错题知识点
        map.put("list5",list5);//错题来源
        map.put("list6",list6);//掌握程度

        return ObjectResult.build("1","成功",map);
    }

    @Override
    public ObjectResult addLabel(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));

        try {
            SLabel label = new SLabel();
            label.setUserId(Integer.parseInt(userId));
            label.setTitle(xmlBean.getTitle());
            label.setType(Integer.parseInt(xmlBean.getType()));
            label.setAddTime(new Date());
            label.setModifyTime(new Date());
            label.setIsDelete(0);
            label.setGradeId(sUser.getGradeId());
            //科目id
            if(!StringUtil.isEmpty(xmlBean.getId())){
                Map<String, Object> map = new HashMap<>();
                map.put("gradeId", sUser.getGradeId());
                map.put("labelId", xmlBean.getId());
                label.setSubjectId(sSubjectMapper.selectSubjectId(map));
            }

            sLabelMapper.insertSelective(label);
        }catch (RuntimeException e){
            ExceptionUtil.logError(e,log);
            return ObjectResult.build("0","添加标签失败");
        }
        return ObjectResult.build("1","添加标签成功");
    }

    @Override
    public ObjectResult questionBankLabel(XMLBean xmlBean) {
//        String userId = CommonUtils.decToken(xmlBean.getToken());


        Map<String,Object>  map = new HashMap<>();
        map.put("subjectId",xmlBean.getId());
        map.put("type","(2,4,9,10)");

        List<SLabel> labels = sLabelMapper.selectWrongTopicLabeLBySubjectId(map);

        List<SLabel> list1 = new ArrayList<>();
        List<SLabel> list2 = new ArrayList<>();
        List<SLabel> list3 = new ArrayList<>();
        List<SLabel> list4 = new ArrayList<>();

        for (SLabel label:labels){
            switch (label.getType()){
                case 2:
                    list1.add(label);break;
                case 4:
                    list2.add(label);break;
                case 9:
                    list3.add(label);break;
                case 10:
                    list4.add(label);break;
                default:break;
            }
        }

        map.clear();
        map.put("list1",list1);
        map.put("list2",list2);
        map.put("list3",list3);
        map.put("list4",list4);

        return ObjectResult.build("1","成功",map);
    }

    @Override
    public ObjectResult offerRewardLabel(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));

        Map<String,Object> map = new HashMap<>();
        List<SLabel> list = new ArrayList<>();
        if ("1".equals(xmlBean.getType())){
            list = sLabelMapper.selectWrongTopicLabelGradeId(sUser.getGradeId());
        }
        if ("2".equals(xmlBean.getType())){
            SLabel label = sLabelMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

            map.put("type","(8)");
            map.put("userId",userId);
            map.put("subject",label.getTitle());
            map.put("gradeId",sUser.getGradeId());
            list = sLabelMapper.selectWrongTopicLabeLByMap(map);

            SLabel label1 = new SLabel();
            label1.setId(0l);
            label1.setTitle("添加标签");
            list.add(label1);
        }

        return ObjectResult.build("1","成功",list);
    }

    @Override
    public ObjectResult wrongTopicScreenLabel(XMLBean xmlBean) {
        String userId =CommonUtils.decToken(xmlBean.getToken());

        Map<String,Object>  map = new HashMap<>();
        map.put("type","(3,5,9,11)");
        map.put("subjectId",xmlBean.getId());
        map.put("userId",userId);

        List<SLabel> labels = sLabelMapper.selectWrongTopicLabeLBySubjectId(map);

        LinkedList<SLabel> list1 = new LinkedList<>();
        LinkedList<SLabel> list2 = new LinkedList<>();
        LinkedList<SLabel> list3 = new LinkedList<>();
        LinkedList<SLabel> list4 = new LinkedList<>();
        LinkedList<SLabel> list5 = new LinkedList<>();
        LinkedList<SLabel> list6 = new LinkedList<>();

        for (SLabel label:labels){
            switch (label.getType()){
                case 2:
                    list5.add(label);break;
                case 3:
                    list4.add(label);break;
                case 4:
                    list3.add(label);break;
                case 5:
                    list6.add(label);break;
                case 9:
                    list2.add(label);break;
                case 11:
                    list1.add(label);break;
                default:break;
            }
        }

        SLabel label1 = new SLabel();
        label1.setId(0l);
        label1.setTitle("全部");
        list1.addFirst(label1);
        list2.addFirst(label1);
        list3.addFirst(label1);
        list4.addFirst(label1);
        list5.addFirst(label1);
        list6.addFirst(label1);

        map.clear();
        map.put("list1",list1);
        map.put("list2",list2);
        map.put("list3",list3);
        map.put("list4",list4);
        map.put("list5",list5);
        map.put("list6",list6);

        return ObjectResult.build("1","成功",map);

    }

    @Override
    public ObjectResult labelUpdate(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SLabel label = sLabelMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        if(label.getUserId() == 0){
            return ObjectResult.build("0","系统标签无法修改");
        }

        if(label.getUserId() != Integer.parseInt(userId)){
            return ObjectResult.build("0","非标签添加者无法修改");
        }

        label.setTitle(xmlBean.getTitle());
        sLabelMapper.updateByPrimaryKeySelective(label);
        return ObjectResult.build("1","修改成功");
    }
}
